package com.soft.face.config.security;


import cn.hutool.extra.spring.SpringUtil;
import com.soft.face.global.Constant;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class TokenStage {

    public static void put(String k, String v) {
        CacheManager cacheManager = SpringUtil.getBean(CacheManager.class);
        Cache cache = cacheManager.getCache("cache");
        cache.put(Constant.TOKEN + k, v);
    }

    public static String get(String k) {
        CacheManager cacheManager = SpringUtil.getBean(CacheManager.class);
        Cache cache = cacheManager.getCache("cache");
        Cache.ValueWrapper value = cache.get(Constant.TOKEN + k);
        return value == null ? null : value.get().toString();
    }

    /**
     * 校验token是否存在，true存在 false不存在
     *
     * @param token
     * @return
     */
    public static boolean check(String token) {
        return !StringUtils.isEmpty(get(token));
    }
}
